package com.example.rabbitmq.comfirm;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;

/**
 * @author： ygl
 * @date： 2019/06/15
 * @Description：
 */
public class MyProducer {

    public static void main(String[] args) throws Exception {

        // 1.创建ConnectionFactory
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        // 2.通过链接工厂创建链接
        Connection connection = connectionFactory.newConnection();
        // 3.通过connection创建channel信道
        Channel channel = connection.createChannel();
        // 4.指定消息投递模式：消息的确认模式
        channel.confirmSelect();
        // 5.通过channel发送数据
        String exchangeName = "test_confirm_exchange";
        String routingKey = "confirm.save";
        String msg = "hello rabbitmq send confirm message!";
        channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());
        // 6.添加一个确认监听
        channel.addConfirmListener(new ConfirmListener() {
            /**
             * @param deliveryTag  消息唯一标签
             * @param multiple
             * @throws IOException
             */
            @Override
            public void handleAck(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("--------ack--------");
            }

            @Override
            public void handleNack(long deliveryTag, boolean multiple) throws IOException {
                System.out.println("--------no ack--------");
            }
        });
        // 6.关闭链接
//        channel.close();
//        connection.close();
    }
}
